package com.android.settings.ios.wallpaper;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.WallpaperManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.settings.ios.R;

public class WallpaperSetActivity extends Activity implements View.OnClickListener {

    private ImageView mImageView;
    private TextView mBtnCancel;
    private TextView mBtnAdd;

    private String mFilePath;
    private Bitmap mCurrentBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.os_activity_wallpaper_set);

        mImageView = findViewById(android.R.id.icon);
        mBtnCancel = findViewById(R.id.btn_set_wall_cancel);
        mBtnAdd = findViewById(R.id.btn_set_wall_add);

        mBtnCancel.setOnClickListener(this);
        mBtnAdd.setOnClickListener(this);

        Intent intent = getIntent();
        mFilePath = intent.getStringExtra("file_path");

        if (mFilePath != null) {
            mCurrentBitmap = BitmapFactory.decodeFile(mFilePath);
            if (mCurrentBitmap != null) {
                mImageView.setImageBitmap(mCurrentBitmap);
            }
        }
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if (R.id.btn_set_wall_add == id) {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setItems(R.array.set_wallpaper_menu_title, new DialogInterface.OnClickListener() {
//
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    onSetWallpaper(which);
//                }
//            });
//            builder.show();
            onSetWallpaper(2);
        } else if (R.id.btn_set_wall_cancel == id) {
            finish();
        }
    }

    private final Handler mHandler = new Handler(Looper.myLooper()) {

        private Dialog mDialog;

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    mDialog = new Dialog(WallpaperSetActivity.this, R.style.IosTransparentLoadingDialogStyle);
                    mDialog.setContentView(R.layout.os_loading_dialog);
                    mDialog.setCanceledOnTouchOutside(false);
                    mDialog.setCancelable(false);
                    mDialog.show();
                    break;
                case 1:
                    if (mDialog != null) {
                        mDialog.dismiss();
                        mDialog = null;
                    }
                    finish();
                    break;
            }
        }
    };

    private void onSetWallpaper(final int which) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                mHandler.sendEmptyMessage(0);
                WallpaperManager wm = getSystemService(WallpaperManager.class);
                try {
                    switch (which) {
                        case 0: // Lock
                            wm.setBitmap(mCurrentBitmap, null, true, WallpaperManager.FLAG_LOCK);
                            break;
                        case 1: // Home
                            wm.setBitmap(mCurrentBitmap, null, true, WallpaperManager.FLAG_SYSTEM);
                            break;
                        case 2: // Both
                            wm.setBitmap(mCurrentBitmap);
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mHandler.sendEmptyMessageDelayed(1, 500);
            }
        }).start();
    }
}
